package com.xianbeiying.app.ckgl.data;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangjie on 16/9/24.
 */
public class PlanUtil {

    public static List<PlanDetailForView> getPlan(JSONObject v, byte taskType) {
        List<PlanDetailForView> views = new ArrayList<PlanDetailForView>();
        JSONArray plans;
        try {
            plans = v.getJSONArray("PLANS");

            if (plans != null && plans.length() > 0)
            {
                for (int i = 0; i < plans.length(); i++)
                {
                    PlanDetailForView view = new PlanDetailForView();
                    byte[] sum = { 0, 0, 0, (byte)plans.length()};
                    int planSum = ServerUtil.byte4ToInt(sum, 0);
                    switch (taskType) {
                        case Cmd.TASK_TYPE_CYCLE_TAP:
                            view.setFarmId(Integer.valueOf(plans.getJSONObject(i).getString("DEVID")));
                            view.setBeginTime(plans.getJSONObject(i).getString("BEGINTIME"));
                            view.setEndTime(plans.getJSONObject(i).getString("ENDTIME"));
                            view.setDuration(plans.getJSONObject(i).getString("DURATION"));
                            view.setPeroid(plans.getJSONObject(i).getString("PEROID"));
                            view.setStatus(Integer.valueOf(plans.getJSONObject(i).getString("STATUS")));
                            view.setSum(planSum);
                            break;

                        case Cmd.TASK_TYPE_TRIGGER_SOIL_HUMI_TAP:
                            view.setFarmId(Integer.valueOf(plans.getJSONObject(i).getString("DEVID")));
                            view.setHumidityMax(Double.valueOf(plans.getJSONObject(i).getString("humidityMax")).intValue());
                            view.setHumidityMin(Double.valueOf(plans.getJSONObject(i).getString("humidityMin")).intValue());
                            view.setDuration(plans.getJSONObject(i).getString("duration"));
                            view.setStatus(Integer.valueOf(plans.getJSONObject(i).getString("STATUS")));
                            view.setSum(planSum);
                            break;

                        case Cmd.TASK_TYPE_TRIGGER_AIR_TEMP_FAN:
                            view.setFarmId(Integer.valueOf(plans.getJSONObject(i).getString("DEVID")));
                            view.setHumidityMax((Double.valueOf(plans.getJSONObject(i).getString("tempMax")).intValue()));
                            view.setHumidityMin(Double.valueOf(plans.getJSONObject(i).getString("tempMin")).intValue());
                            view.setDuration(plans.getJSONObject(i).getString("duration"));
                            view.setStatus(Integer.valueOf(plans.getJSONObject(i).getString("STATUS")));
                            view.setSum(planSum);
                            break;
                        case Cmd.TASK_TYPE_CYCLE_GROW_LIGHT:
                            view.setFarmId(Integer.valueOf(plans.getJSONObject(i).getString("DEVID")));
                            view.setBeginTime(plans.getJSONObject(i).getString("BEGINTIME"));
                            view.setEndTime(plans.getJSONObject(i).getString("ENDTIME"));
                            view.setDuration(plans.getJSONObject(i).getString("DURATION"));
                            view.setPeroid(plans.getJSONObject(i).getString("PEROID"));
                            view.setStatus(Integer.valueOf(plans.getJSONObject(i).getString("STATUS")));
                            view.setSum(planSum);
                            break;
                        default:
                            break;
                    }
                    views.add(view);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return views;
    }
}
